﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace PeopleEditor
{
    /// <summary>
    /// Common administration methods for the PeoplePickers
    /// </summary>
    public static class Common
    {
        #region Public methods
        /// <summary>
        /// Creates the persistent Common and Admin objects for the web
        /// </summary>
        /// <param name="webApp"></param>
        public static void CreatePersistentObjects(SPWebApplication webApp)
        {
            //Add common settings to the data store
            createCommonSettings(webApp);
            createAdminSettings(webApp);
        }

        /// <summary>
        /// Updates the persistent object
        /// </summary>
        /// <param name="webApp"></param>
        /// <param name="settings"></param>
        public static void UpdatePersistentObject(SPWebApplication webApp, PeoplePickerSettings settings)
        {
            //SPContext.Current.Site.AllowUnsafeUpdates = true;
            
            settings.Update();

            //Serialize and propagate changes across farm
            webApp.Update();
        }

        /// <summary>
        /// Creates a comma separated string from a listitem collection
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string CreateStringFromSelections(ListItemCollection items)
        {
            string returnValue = "";
            foreach (ListItem item in items)
            {
                if (item.Selected)
                {
                    returnValue += String.Format("{0},", item.Value);
                }
            }
            //trim the last comma
            returnValue = returnValue.TrimEnd(Globals.Delimiter);

            return returnValue;
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Returns the PeoplePicker common settings
        /// </summary>
        public static PeoplePickerSettings CommonPeoplePickerSettings(SPWebApplication webApp)
        {
            PeoplePickerSettings commonSettings = webApp.GetChild<PeoplePickerSettings>(Globals.CommonSettingsName);
            if (commonSettings == null)
            {
                createCommonSettings(webApp);
                commonSettings = webApp.GetChild<PeoplePickerSettings>(Globals.CommonSettingsName);
            }
            return commonSettings;
        }

        /// <summary>
        /// Returns the PeoplePicker admin settings
        /// </summary>
        public static PeoplePickerSettings AdminPeoplePickerSettings(SPWebApplication webApp)
        {
            PeoplePickerSettings adminSettings = webApp.GetChild<PeoplePickerSettings>(Globals.AdminSettingsName);
            if (adminSettings == null)
            {
                createAdminSettings(webApp);
                adminSettings = webApp.GetChild<PeoplePickerSettings>(Globals.AdminSettingsName);
            }
            return adminSettings;
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Creates the common settings object
        /// </summary>
        /// <param name="webApp"></param>
        private static void createCommonSettings(SPWebApplication webApp)
        {
            //SPContext.Current.Site.AllowUnsafeUpdates = true;
            PeoplePickerSettings commonSettings = webApp.GetChild<PeoplePickerSettings>(Globals.CommonSettingsName);
            if (commonSettings == null)
            {
                SPPeoplePickerSettings webAppPeopleSettings = webApp.PeoplePickerSettings;
                commonSettings = new PeoplePickerSettings(Globals.CommonSettingsName, webApp, defaultSelectionSet, webAppPeopleSettings.OnlySearchWithinSiteCollection);
                commonSettings.Update();

                //Serialize and propagate changes across farm
                webApp.Update();
            }
        }

        /// <summary>
        /// Creates the admin settings object
        /// </summary>
        /// <param name="webApp"></param>
        private static void createAdminSettings(SPWebApplication webApp)
        {
            //SPContext.Current.Web.AllowUnsafeUpdates = true;
            //SPContext.Current.Site.AllowUnsafeUpdates = true;
            PeoplePickerSettings adminSettings = webApp.GetChild<PeoplePickerSettings>(Globals.AdminSettingsName);
            if (adminSettings == null)
            {
                SPPeoplePickerSettings webAppPeopleSettings = webApp.PeoplePickerSettings;
                adminSettings = new PeoplePickerSettings(Globals.AdminSettingsName, webApp, PeoplePickerSettings.AdminType.SiteAdmin, defaultSelectionSet, webAppPeopleSettings.OnlySearchWithinSiteCollection);
                adminSettings.Update();

                //Serialize and propagate changes across farm
                webApp.Update();
            }
        }
        #endregion

        #region Private properties
        /// <summary>
        /// Creates a default SelectionSet string based on the 
        /// Microsoft.SharePoint.WebControls.PeopleEditor.AccountType enum
        /// </summary>
        /// <returns></returns>
        private static string defaultSelectionSet
        {
            get
            {
                string selectionSet = "";
                String[] accountTypes = Enum.GetNames(typeof(PeopleEditor.CustPeopleFind.AccountType));
                foreach (string s in accountTypes)
                {
                    selectionSet += String.Format("{0},", s);
                }
                //trim the last comma
                selectionSet = selectionSet.TrimEnd(Globals.Delimiter);

                return selectionSet;
            }
        }
        #endregion
    }
}